//
//  GAOEmoticonCell.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
class GAOEmoticonCell: UICollectionViewCell {
    
    /// 表情数据模型
    var emoticons: [GAOEmoticonModel]? {
        didSet {
            //保证数据有值
            guard let data = emoticons else {
                return
            }
            // 隐藏所有按钮,赋值时再显示,解决因cell重用造成表情重复显示的问题
            for button in EmoticonButtons {
                button.hidden = true
            }
            // 遍历赋值
            for (i, value) in data.enumerate() {
                // 通过下标取到button
                let button = EmoticonButtons[i]
                // 显示取到的button
                button.hidden = false
                // 给button的模型赋值
                button.dataModel = value
            }
            
        }
    }
    
    var indexPath: NSIndexPath? {
        didSet {
            
            label.text = "第\(indexPath!.section)组,第\(indexPath!.item)个item"
            
        }
    }
    
    private lazy var label: UILabel = UILabel(textColor: UIColor.whiteColor(), fontSize: 20)
    
    //  保存表情button的数组
    private lazy var EmoticonButtons: [GAOEmoticonButton] = [GAOEmoticonButton]()
    
    //  删除按钮
    private lazy var deleteEmoticonButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        button.addTarget(self, action: "deleteEmoticonButtonAction", forControlEvents: .TouchUpInside)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        addChildButton()
        
        //        contentView.addSubview(label)
        //
        //        label.snp_makeConstraints { (make) -> Void in
        //            make.center.equalTo(self)
        //        }
    }
    
    //添加表情按钮
    private func addChildButton() {
        // 循环创建20个表情
        for _ in 0..<20 {
            let button = GAOEmoticonButton()
            //添加点击事件
            button.addTarget(self, action: "emoticonButtonAction:", forControlEvents: .TouchUpInside)
            //            button.backgroundColor = RandomColor()
            contentView.addSubview(button)
            //添加到表情数组中
            EmoticonButtons.append(button)
        }
        contentView.addSubview(deleteEmoticonButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置表情按钮的frame
        
        let itemWidth = width / 7
        let itemHeight = height / 3
        
        for (i, value) in EmoticonButtons.enumerate() {
            //设置按钮大小
            value.size = CGSize(width: itemWidth, height: itemHeight)
            // 计算列的索引
            let colIndex = i % 7
            // 计算行的索引
            let rowIndex = i / 7
            // x坐标
            value.x = CGFloat(colIndex) * itemWidth
            // y坐标
            value.y = CGFloat(rowIndex) * itemHeight
        }
        
        // 设置删除按钮的frame
        deleteEmoticonButton.size = CGSize(width: itemWidth, height: itemHeight)
        // x坐标
        deleteEmoticonButton.x = width - itemWidth
        // y坐标
        deleteEmoticonButton.y = height - itemHeight
    }
    
    @objc private func emoticonButtonAction(sender: GAOEmoticonButton) {
         let emoticonData = sender.dataModel
        
         NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmoticonNotification, object: emoticonData)
    }
    
    @objc private func deleteEmoticonButtonAction() {
        NSNotificationCenter.defaultCenter().postNotificationName(DeleteEmoticonButtonNotification, object: nil)
    }
    
}
